package me.algo.lst;

import org.checkerframework.checker.units.qual.Luminance;

/**
 * @author guozheng
 * @date 2024/4/20
 */
public class L4 {
    public static void main(String[] args) {
        ListNode node = new ListNode(1, new ListNode(4, new ListNode(3, new ListNode(2, new ListNode(5, new ListNode(2))))));
        ListNode partition = partition(node, 3);
        System.out.println(partition);
    }

    /**
     * 86. 分隔链表
     * @param head
     * @param x
     * @return
     */
    public static ListNode partition(ListNode head, int x) {
        ListNode small = new ListNode();
        ListNode scur = small;
        ListNode large = new ListNode();
        ListNode lcur = large;
        while (head != null) {
            if (head.val < x) {
                scur.next = head;
                scur = scur.next;
                head = head.next;
                scur.next = null;
            } else {
                lcur.next = head;
                lcur = lcur.next;
                head = head.next;
                lcur.next = null;
            }
        }
        scur.next = large.next;
        return small.next;
    }
}
